package xyz.liut.fragmentdialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

/**
 * @author Liut
 * @ProjectName GlobalCatcher
 * @Description MyAlertDialog
 * @date 2016/8/1
 */
public class MyDialog extends DialogFragment {

    private static final String TITLE = "title";
    private static final String MESSGAE = "message";
    private static final String POS = "pos";
    private static final String NEG = "neg";
    private static final String CANCELABLE = "cancelable";
    private static MyDialog fragment;

    private DialogInterface.OnClickListener mListener;

    public static MyDialog getInstance(String title, String message, String pos, String neg, boolean cancleable, Dialog.OnClickListener listener) {
        fragment = new MyDialog();
        fragment.mListener = listener;
        Bundle args = new Bundle();
        args.putString(TITLE, title);
        args.putString(MESSGAE, message);
        args.putString(POS, pos);
        args.putString(NEG, neg);
        args.putBoolean(CANCELABLE, cancleable);
        fragment.setArguments(args);
        return fragment;
    }

    public void show(Activity activity){
        fragment.show(activity.getFragmentManager(),activity.getLocalClassName());
    }

    @SuppressLint("ValidFragment")
    private MyDialog() {
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
                .setTitle(getArguments().getString(TITLE))
                .setMessage(getArguments().getString(MESSGAE))
                .setPositiveButton(getArguments().getString(POS), mListener)
                .setNegativeButton(getArguments().getString(NEG), mListener)
                .setCancelable(getArguments().getBoolean(CANCELABLE))
                .show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mListener = null;
    }
}
